package com.qiwx.pigeon.Utils;


import android.text.TextUtils;


import java.text.DecimalFormat;
import java.util.regex.Pattern;


public class NumberUtil {
    public static final int MONEY_DECIMAL_PART = 2;//钱小数长度

    //double 直接拼接 字符串 会"%g" 去格式化 变成科学计数法计数
    public static String formatMoney(double money) {
        return String.format("%." + getDecimalPartCount(money) + "f", money);
    }

    //double 直接拼接 字符串 会"%g" 去格式化 变成科学计数法计数，  保留小数点后2位
    public static String format2Money(double money) {
        return String.format("%.2f", money);
    }

    public static String formatMoney(float money) {
        return String.format("%." + getDecimalPartCount(money) + "f", money);
    }

    public static String formatMoneyTo10K(double money) {
        double m10k = money / 10000;
        int count = getDecimalPartCount(m10k);
        return String.format("%." + count + "f", m10k);
    }

    /**
     * 有些字段 在后台录入和展示的约束不一样  导致数据在展示的时候丢失精度
     * 计算小数部分长度
     *
     * @param money
     * @return
     * @author: zhouzc
     */
    private static int getDecimalPartCount(double money) {
        String sM = String.valueOf(money);
        return getDecimalPartCount(sM);
    }

    private static int getDecimalPartCount(String decinalNumber) {
        int count = getRealDecimalPartCount(decinalNumber);
        if (count < MONEY_DECIMAL_PART) {//如果小数部分长度小于 Constant.MONEY_DECIMAL_PART
            count = MONEY_DECIMAL_PART;
        }
        return count;
    }

    public static int getRealDecimalPartCount(String decimalNumber) {
        int index = decimalNumber.indexOf(".");
        int count = -1;
        if (index >= 0) {//有小数部分
            count = decimalNumber.length() - (index + 1);
        }
        return count;
    }

    /**
     * 里程数转换成万公里
     *
     * @param strMileage
     * @return
     */
    public static String formatMileage(String strMileage) {
        if (TextUtils.isEmpty(strMileage)) {
            return "";
        }
        try {
            float mileage = Float.parseFloat(strMileage);
            return String.format("%.2f", mileage / 10000);
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
            return "";
        }
    }

    /**
     * 里程数转换成万公里
     *
     * @param strMileage
     * @return
     */
    public static String formatDoubleMileage(String strMileage) {
        if (TextUtils.isEmpty(strMileage)) {
            return "";
        }
        try {
            float mileage = Float.parseFloat(strMileage);
            return String.format("%.2f", mileage);
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
            return "";
        }
    }

    /**
     * 格式化价格小于一万直接显示，大于等于一万显示完元
     *
     * @param price
     * @return
     */
    public static String formatPrice(int price) {
        price /= 100; //后台反回的是分
        if (price < 10000) {
            return String.format("%d元", price);
        } else {
            return String.format("%.2f万元", price / 10000f);
        }
    }

    /**
     * 转换数字为小数 为空时显示--
     *
     * @param strNum
     * @return
     */
    public static String formatNumber(String strNum) {
        if (TextUtils.isEmpty(strNum)) {
            return "--";
        }
        try {
            float number = Float.parseFloat(strNum);
            String result = String.format("%.2f", number);
//            if (result.endsWith(".00")) {
//                result = result.substring(0, result.indexOf(".00"));
//            }
            return result;
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
            return "--";
        }
    }

    //将Double 类型保留两位，如果为2.0 显示整形
    public static String showDouble(double d) {
        String result = null;
        DecimalFormat df = new DecimalFormat("######0.00");
        result = df.format(d);
        if (result.indexOf(".") > 0) {
            result = result.replaceAll("0+?$", "");//去掉多余的0
            result = result.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return result;
    }

    //将String类型的数值格式化
    public static String showDouble(String str) {
        String result = null;
        if (str != null) {
            try {
                Double d = Double.valueOf(str);
                result = showDouble(d);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            result = str;
        }
        return result;
    }

    //字符串转int
    public static int parseInt(String s) {
        if (s == null || s.length() == 0)
            return 0;
        try {
            return Integer.valueOf(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //判断一个字符串是否大于0
    public static boolean isBiggerZero(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        int num = 0;
        try {
            num = Integer.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num > 0;
    }

    public static boolean isNumeric(String str) {
        if (str == null)
            return false;
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
    //检测鸽笼编号是否正确
    public static int checkCageNum(String edtNum) {
        if (StringUtil.isEmpty(edtNum)) {
            MyToast.showLong("鸽笼编号不能为空！");
        }
        int num = NumberUtil.parseInt(edtNum);
        return num;
    }
}
